<?php
use I\Table;
use M\Middle_Public;
use C\Config_App;

class Controller extends Middle_Public {

    public function checkAuth() {
    }

    public function index() {
        return $this->view->redirect('/task/ido');
    }

    public function loginform() {
        return $this->view->html('login');
    }

    public function login() {
        $account = getgpc('account');
        if (!$account) {
            return $this->view->html('login');
        }

        $password = getgpc('password');
        if (!$password) {
            return $this->view->html('login');
        }

        $user = Table::singleton('users')->findone(['account' => $account]);
        if (!$user) {
            $this->view->error('不存在');
        }

        if (!password_verify($password, $user->password)) {
            $this->view->error('密码不正确');
        }

        $segment = $this->getsegment();
        $segment->set('authed', $user);

        return $this->view->redirect('/task/ido');
    }

    public function logout() {
        $segment = $this->getsegment();
        $segment->set('authed', NULL);

        $this->view->html('login');
    }

    //飞书登录
    public function flogin() {
        $code = getgpc('code');
        $error = getgpc('error', '');
        if ($error) {
            echo 'error';
            return;
        }
        if (!$code) {
            echo 'no code';
            return;
        }

         // 请求地址
        $url = 'https://open.feishu.cn/open-apis/authen/v2/oauth/token';

        // 请求数据
        $data = [
            'grant_type' => 'authorization_code',
            'code' => $code,
            'client_id' => Config_App::feishu_client_id, // 替换为你的 client_id
            'client_secret' => Config_App::feishu_client_secret, // 替换为你的 client_secret
            'redirect_uri' => Config_App::feishu_redirect_uri,
        ];

        // 初始化 cURL
        $ch = curl_init($url);

        // 设置 cURL 选项
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json; charset=utf-8'
        ]);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // 执行 cURL 请求
        $response = curl_exec($ch);

        // 检查是否有错误
        if (curl_errno($ch)) {
            echo 'Curl error: ' . curl_error($ch);
        }

        // 关闭 cURL
        curl_close($ch);

        // 处理响应
        $result = json_decode($response);
        if ($result->code == 0) {
            // 处理响应数据
            $access_token = $result->access_token;
            // 下一步操作，例如获取用户信息
            $user_info_url = 'https://open.feishu.cn/open-apis/authen/v1/user_info';
            $user_info_ch = curl_init($user_info_url);
            curl_setopt($user_info_ch, CURLOPT_HTTPHEADER, [
                'Authorization: Bearer ' . $access_token,
                'Content-Type: application/json; charset=utf-8'
            ]);
            curl_setopt($user_info_ch, CURLOPT_RETURNTRANSFER, true);
            $user_info_response = curl_exec($user_info_ch);
            curl_close($user_info_ch);
            $user_info = json_decode($user_info_response);
            if ($user_info->code == 0) {
                // echo $user_info_response;

                $this->checkuser($user_info->data);
            } else {
                // 处理错误
                echo $user_info_response;
            }
        } else {
            // 处理错误
            echo $response;
        }
    }

    private function checkuser($user_info) {
        $user = Table::singleton('users')->findone([
            'account' => $user_info->user_id
        ]);

        if (!$user) {
            $data = [
                'name' => $user_info->name,
                'account' => $user_info->user_id,
                'password' => password_hash($user_info->user_id, PASSWORD_DEFAULT),
                'department' => 306
            ];
            $id = Table::singleton('users')->insert($data);
            $data['id'] = $id;
            $user = (object) $data;
        }

        $segment = $this->getsegment();
        $segment->set('authed', $user);

        return $this->view->redirect('/task/ido');
    }

}
